<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ZPituitaryUser extends Model
{
    protected $table = 'z_pituitary_user';

    protected $guarded = ['id'];


    /**
     * 获取入组用户
     *
     * @param $hospId     '医院id'
     * @param $deptId     '科室id'
     * @param bool $startDate  开始时间
     * @param bool $endDate    结束时间
     * @param bool $selectUser 是否查询用户信息
     *
     * @return array
     */
    public static function getGroupUserList($hospId, $deptId, $startDate = false, $endDate = false, $selectUser = false)
    {

        return self::query()->from('z_pituitary_user as pu')
            ->selectRaw('pu.user_id,pu.hosp_id,pu.dept_id,pu.created_at')
            ->join('z_pituitary_visit_records as r', function($query) use($hospId, $deptId) {
                    $query->on('r.user_id','=','pu.user_id')
                        ->where('r.hosp_id', $hospId)
                        ->where('r.dept_id', $deptId);
            })
            ->when($startDate, function($query) use ($startDate) {
                return $query->where('pu.created_at','>=', $startDate);
            })
            ->when($endDate, function($query) use ($endDate) {
                return $query->where('pu.created_at','<=', $endDate);
            })
            ->groupBy('pu.user_id')
            ->get()->toArray();

    }

}